﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using System.Xml.Linq;
using System.Reflection.Metadata;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using Castle.Core.Resource;
using MailKit.Search;
using Mysqlx;

namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    ///  组件CRM签收信息更新
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CRMSignInforUpdateController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con_zh;
        public readonly string con_qh;
        public readonly string con_yb;
        public readonly string con_oa;
        public readonly string gz_con;
        public readonly string erp_con;


        public CRMSignInforUpdateController(ILogger<CRMSignUploadRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            con_zh = _configuration.GetConnectionString("WMSDbConnection_ZH");
            con_qh = _configuration.GetConnectionString("WMSDbConnection");
            con_yb = _configuration.GetConnectionString("WMSDbConnection_YB");
            con_oa = _configuration.GetConnectionString("OA_DbConnection");
            gz_con = _configuration.GetConnectionString("GZ_WMS_DbConnection");
            erp_con = _configuration.GetConnectionString("ERP_CRMDbConnection");
            _logger = loggerFactory.CreateLogger<CRMSignInforUpdateController>();
        }

        /// <summary>
        /// 组件CRM签收信息更新
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostSignInforUpdate([FromBody] CRMPriceReportRequestDto strparm)
        {
            string strMsg = "";
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            CRMPriceReportResponseDto re = new CRMPriceReportResponseDto();
            
            try
            {
                dic = new Dictionary<string, string>();
                dic.Add("source_no", "");
                MethodHelp.InsertIntoTable("TMS0174", dic, con);

                dic = new Dictionary<string, string>();
                dic.Add("source_no", "");
                MethodHelp.InsertIntoTable("TMS0175", dic, con);

                dic = new Dictionary<string, string>();
                dic.Add("source_no", "");
                MethodHelp.InsertIntoTable("TMS0176", dic, con);

                dic = new Dictionary<string, string>();
                dic.Add("source_no", "");
                MethodHelp.InsertIntoTable("TMS0177", dic, con);



                //发货自提，更新出库日期、出库单号
                dic = new Dictionary<string, string>();
                dic.Add("source_no", "");
                DataTable zt_dt = MethodHelp.GetTableInfo("TMS0221", dic, con);
                if (zt_dt.Rows.Count > 0)
                {
                    foreach (DataRow item in zt_dt.Rows)
                    {
                        dic = new Dictionary<string, string>(); //查询公用dic
                        dic.Add("SOREFERENCE3", item["djh"].ToString());
                        string strSql2 = MethodHelp.GetTranSQL("TMS0220", dic, con);//获取WMS送货数据SQL
                        if (strSql2 != "")
                        {
                            DataTable dt_cb = DB.OracleQuery(strSql2, gz_con);
                            if (dt_cb.Rows.Count > 0)
                            {
                                dic = new Dictionary<string, string>(); //查询公用dic
                                dic.Add("ship_date", dt_cb.Rows[0]["EDITTIME"].ToString());
                                dic.Add("shiporder_no", dt_cb.Rows[0]["ORDERNO"].ToString());
                                dic.Add("djh", item["djh"].ToString());
                                string strSql3 = MethodHelp.GetTranSQL("TMS0222", dic, con);//获取WMS送货数据SQL
                                if (strSql3 != "")
                                {
                                    strSqlList.Add(strSql3);
                                }

                            }
                        }
                    }
                    if (strSqlList.Count > 0)
                    {
                        bool A = DB.ExecuteNoQueryTran(strSqlList, con);
                        if (A)
                        {

                        }
                        else
                        {
                            strMsg = "Database update failed";
                        }
                    }
                }


                //签收台账更新ERP凭证事务处理ID
                dic = new Dictionary<string, string>();
                dic.Add("source_no", "");
                DataTable qs_dt = MethodHelp.GetTableInfo("TMS0225", dic, con);
                if (qs_dt.Rows.Count > 0)
                {
                    foreach (DataRow item in qs_dt.Rows)
                    {
                        dic = new Dictionary<string, string>(); //查询公用dic
                        dic.Add("ORDER_NUMBER", item["sales_order_number"].ToString());
                        dic.Add("LINE_NUMBER", item["erp_transaction_id"].ToString());
                        string strSql2 = MethodHelp.GetTranSQL("TMS0224", dic, con);//获取WMS送货数据SQL
                        if (strSql2 != "")
                        {
                            DataTable dt_cb = DB.OracleQuery(strSql2, erp_con);
                            if (dt_cb.Rows.Count > 0)
                            {
                                dic = new Dictionary<string, string>(); //查询公用dic
                                dic.Add("code", item["code"].ToString());
                                dic.Add("transaction_id", dt_cb.Rows[0]["TRX_NUMBER"].ToString());
                                string strSql3 = MethodHelp.GetTranSQL("TMS0226", dic, con);//获取WMS送货数据SQL
                                if (strSql3 != "")
                                {
                                    strSqlList.Add(strSql3);
                                }

                            }
                        }
                    }
                    if (strSqlList.Count > 0)
                    {
                        bool A = DB.ExecuteNoQueryTran(strSqlList, con);
                        if (A)
                        {

                        }
                        else
                        {
                            strMsg = "Database update failed";
                        }
                    }
                }




            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

            if (strMsg == "")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;

            }


            //_logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);

        }


    }
}
